package com.zy.controller;

import com.zy.domain.AcceptTask;
import com.zy.domain.ReleaseTask;
import com.zy.domain.User;
import com.zy.param.AcceptTaskParam;
import com.zy.param.RegisterParam;
import com.zy.param.ReleaseTaskParam;
import com.zy.service.AcceptTaskService;
import com.zy.service.ReleaseTaskService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
public class TaskController {

  @Autowired
  private ReleaseTaskService releaseTaskService;
  @Autowired
  private AcceptTaskService acceptTaskService;

  @GetMapping("/task")
  public String Test() {
    return "ok";
  }

  @ApiOperation("通过发布的任务id获取任务")
//  @ApiImplicitParam(name =,value = )
  @GetMapping("/getTaskById/{id}")
  public ReleaseTask getTaskById( @PathVariable Integer id){
    return releaseTaskService.getTaskById(id);
  }

  @ApiOperation("接受任务")
  @PostMapping("/acceptTask")
  public ResponseEntity<String> acceptTask(
    @RequestBody  AcceptTaskParam acceptTaskParam
  ) {
      checkAcceptTaskParam(acceptTaskParam);
    AcceptTask acceptTask = acceptTaskService.acceptTask(acceptTaskParam);
    if (acceptTask==null){
      return ResponseEntity.ok("接受任务失败！");
    }
    else {
      return ResponseEntity.ok("接受任务成功！");
    }
  }

  private void checkAcceptTaskParam(AcceptTaskParam acceptTaskParam) {
    Integer rid = acceptTaskParam.getRid();
    String acceptUserId = acceptTaskParam.getAcceptUserId();
//    String releaseUserId = acceptTaskParam.getReleaseUserId();
    if (rid==null){
      throw  new RuntimeException("接受的任务异常！");
    }
    if (acceptUserId==null){
      throw  new RuntimeException("接受任务的用户异常！");
    }
//    if (releaseUserId==null){
//      throw  new RuntimeException("发布的任务异常！");
//    }
  }

  @ApiOperation("发布任务")
  @PostMapping("/releaseTask")
  public ResponseEntity<String> releaseTask(
    @RequestBody ReleaseTaskParam releaseTaskParam
  ) {
    checkReleaseTaskParam(releaseTaskParam);
    ReleaseTask releaseTask = releaseTaskService.releaseTask(releaseTaskParam);
    if (releaseTask==null){
      return ResponseEntity.ok("发布失败！");
    }
    else {
      return ResponseEntity.ok("发布成功！");
    }
//    User user = releaseTaskService.selectUserByPhone(registerParam.getPhoneNum());
//    if (user!=null){
//      throw  new RuntimeException("用户已注册！");
//    }
//    User newUser = userService.registerUser(registerParam);
//    System.out.println("注册用户为："+newUser);
//    return ResponseEntity.ok("注册成功！");
  }

  private void checkReleaseTaskParam(ReleaseTaskParam releaseTaskParam) {
//订单名称
    String name = releaseTaskParam.getName();
    if (name==null){
      throw  new RuntimeException("请输入订单名称!");
    }
//任务类型 1.投票;2.注册;3.下载;4.转发;5.发帖;6.特殊;7.关注;8.其他
    Integer type = releaseTaskParam.getType();
//数量
    Integer num = releaseTaskParam.getNum();
    if (num==null){
      throw  new RuntimeException("请输入任务数量!");
    }
//单价
    Double money = releaseTaskParam.getMoney();
    if (money==null){
      throw  new RuntimeException("请输入任务的单价!");
    }
//合计
//链接
    String url = releaseTaskParam.getUrl();
//备注
    String remarks = releaseTaskParam.getRemarks();
    if (remarks.length()>50||remarks==null){
      throw  new RuntimeException("请输入备注并且不能多于50字！");
    }
//截止时间
    Date endTime = releaseTaskParam.getEndTime();
    if (endTime==null){
      throw  new RuntimeException("请输入任务截止的时间!");
    }
//审核图样
    String img01 = releaseTaskParam.getImg01();
    String img02 = releaseTaskParam.getImg02();
    String img03 = releaseTaskParam.getImg03();
    String img04 = releaseTaskParam.getImg04();
    if (img01==null&&img02==null&&img03==null&&img04==null){
      throw  new RuntimeException("请上传审核图样!");
    }
//操作说明
    String oitext01 = releaseTaskParam.getOitext01();
    String oitext02 = releaseTaskParam.getOitext02();
    String oitext03 = releaseTaskParam.getOitext03();
    String oitext04 = releaseTaskParam.getOitext04();
    String oitext05 = releaseTaskParam.getOitext05();
    if (oitext01==null&&oitext02==null&&oitext03==null&&oitext04==null&&oitext05==null){
      throw  new RuntimeException("请说明操作!");
    }
  }


}
